home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / wfc007.000 / src / lzfile.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-22  |  3.8 KB  |  183 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like.
  10. */
  11.  
  12. #if defined( _DEBUG )
  13. #undef THIS_FILE
  14. static char BASED_CODE THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. CLZFile::CLZFile()
  18. {
  19.    m_Initialize();
  20. }
  21.  
  22. CLZFile::~CLZFile()
  23. {
  24.    TRACE( "Destroying a CLZFile object\n" );
  25.    Close();
  26. }
  27.  
  28. void CLZFile::Close( void )
  29. {
  30.    ASSERT_VALID( this );
  31.  
  32.    if ( m_LZFileHandle >= 0 )
  33.    {
  34.       ::LZClose( m_LZFileHandle );
  35.       m_LZFileHandle = LZERROR_BADINHANDLE;
  36.       m_hFile = CFile::hFileNull;
  37.    }
  38.  
  39.    m_Initialize();
  40. }
  41.  
  42. BOOL CLZFile::Copy( const CLZFile * source_p )
  43. {
  44.    return( Copy( *source_p ) );
  45. }
  46.  
  47. BOOL CLZFile::Copy( const CLZFile& source )
  48. {
  49.    LONG size_of_destination_file = 0;
  50.  
  51.    size_of_destination_file = ::LZCopy( source.m_LZFileHandle, m_LZFileHandle );
  52.  
  53.    if ( size_of_destination_file < 0 )
  54.    {
  55.       m_ErrorCode = size_of_destination_file;
  56.       return( FALSE );
  57.    }
  58.  
  59.    return( TRUE );
  60. }
  61.  
  62. BOOL CLZFile::GetExpandedName( LPTSTR name_of_compressed_file, CString& original_file_name )
  63. {
  64.    TCHAR file_name[ _MAX_PATH ];
  65.  
  66.    ::ZeroMemory( file_name, sizeof( file_name ) );
  67.  
  68.    if ( ::GetExpandedName( name_of_compressed_file, file_name ) == 1 )
  69.    {
  70.       original_file_name = file_name;
  71.       return( TRUE );
  72.    }
  73.    else
  74.    {
  75.       original_file_name.Empty();
  76.       return( FALSE );
  77.    }
  78. }
  79.  
  80. void CLZFile::m_Initialize( void )
  81. {
  82.    m_LZFileHandle = 0;
  83.    m_OpenFileStructure.Empty();
  84. }
  85.  
  86. BOOL CLZFile::Open( const char *file_name, UINT style, CFileException * error_p )
  87. {
  88.    m_LZFileHandle = ::LZOpenFile( (char *) file_name, &m_OpenFileStructure, style );
  89.  
  90.    if ( m_LZFileHandle < 0 )
  91.    {
  92.       m_ErrorCode = m_LZFileHandle;
  93.  
  94.       return( FALSE );
  95.    }
  96.  
  97.    return( TRUE );
  98. }
  99.  
  100. UINT CLZFile::Read( void *buffer, UINT size_of_buffer )
  101. {
  102.    INT number_of_bytes_read = 0;
  103.  
  104.    number_of_bytes_read = ::LZRead( m_LZFileHandle, (char *) buffer, size_of_buffer );
  105.  
  106.    if ( number_of_bytes_read < 0 )
  107.    {
  108.       m_ErrorCode = number_of_bytes_read;
  109.       return( 0 );
  110.    }
  111.  
  112.    return( (UINT) number_of_bytes_read );
  113. }
  114.  
  115. LONG CLZFile::Seek( LONG offset, UINT from )
  116. {
  117.     ASSERT_VALID( this );
  118.     ASSERT( m_hFile != (UINT) CFile::hFileNull );
  119.     ASSERT( from == begin || from == end || from == current);
  120.     ASSERT( begin == FILE_BEGIN && end == FILE_END && current == FILE_CURRENT );
  121.  
  122.     LONG offset_from_beginning_of_file = ::LZSeek( m_LZFileHandle, offset, from );
  123.  
  124.    if ( offset_from_beginning_of_file < 0 )
  125.    {
  126.       m_ErrorCode = offset_from_beginning_of_file;
  127.    }
  128.  
  129.     return( offset_from_beginning_of_file );
  130. }
  131.  
  132. void CLZFile::TranslateErrorCode( int error_code, CString& error_message )
  133. {
  134.    switch( error_code )
  135.    {
  136.       case LZERROR_BADINHANDLE:
  137.  
  138.          error_message = "Invalid input handle";
  139.          return;
  140.  
  141.       case LZERROR_BADOUTHANDLE:
  142.  
  143.          error_message = "Invalid output handle";
  144.          return;
  145.  
  146.       case LZERROR_READ:
  147.  
  148.          error_message = "Corrupt compressed file format";
  149.          return;
  150.  
  151.       case LZERROR_WRITE:
  152.  
  153.          error_message = "Out of space for output file";
  154.          return;
  155.  
  156.       case LZERROR_GLOBALLOC:
  157.  
  158.          error_message = "Insufficient memory for LZFile struct";
  159.          return;
  160.  
  161.       case LZERROR_GLOBLOCK:
  162.  
  163.          error_message = "Bad global handle";
  164.          return;
  165.  
  166.       case LZERROR_BADVALUE:
  167.  
  168.          error_message = "Input parameter out of acceptable range";
  169.          return;
  170.  
  171.       case LZERROR_UNKNOWNALG:
  172.  
  173.          error_message = "Compression algorithm not recognized";
  174.          return;
  175.  
  176.       default:
  177.  
  178.          error_message.Format( "Unknown error number %d", error_code );
  179.          return;
  180.    }
  181. }
  182.  
  183.